import VueRouter from "vue-router";
import Vue from "vue";
import routes from "./routes";
import NProgress from "nprogress";
import "nprogress/nprogress.css";

// 插件使用
Vue.use(VueRouter);

// 重写push
const oldPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (
  location,
  resolve = () => {},
  reject = () => {}
) {
  oldPush.call(this, location, resolve, reject);
};

const router = new VueRouter({
  routes,
  mode: "history",
});

// 全局前置守卫，设置进度条
router.beforeEach((to, from, next) => {
  NProgress.start();
  next();
});

// 全局后置钩子，结束进度条
router.afterEach(() => {
  NProgress.done();
});

export default router;
